﻿#light

module Dwarf_BranchAndBound_Generator

open System
open System.IO
open System.Text
open System.ComponentModel

let Dwarf_BranchAndBound_Generator (fs : FileStream, args : String[]) = 

    // subsidiary objects
    let writer = new StreamWriter(fs);
    let rand = new Random();

    // current data values
    let count_of_kilobytes = (int) args.[args.Length - 1]
    let mutable max_value = 10000    
    let mutable current_dimension = 0

    for i = 0 to args.Length - 2 do     // search for user-defined dimension of square matrix and max_value
        if (args.[i] = "-dim") then current_dimension <- (int) args.[i + 1]
        if (args.[i] = "-max") then max_value <- (int) args.[i + 1]

    if (current_dimension = 0) then     // if there no user-defined dimension in args, evaluate the defaults from size
        let average_byte_count = 4 + 1  // 4 bytes for digits, 1 byte for ' ' between two values in row
        let count_of_digits_in_kilobyte = 1024
        let division x = (float) (x * count_of_digits_in_kilobyte / average_byte_count)
        let dimension x = (int) (Math.Round(Math.Sqrt(division x)))
        current_dimension <- dimension count_of_kilobytes

    // write the dimension
    writer.Write(current_dimension);
    writer.Write('\n');

    for i = 0 to current_dimension - 1 do
        for j = 0 to current_dimension - 1 do 

            // write the next random value
            writer.Write(rand.Next(max_value));
            writer.Write(' ');
        writer.Write('\n');

    writer.Close()

    true